package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dto.*;
import com.woniuxy.entity.*;
import com.woniuxy.mapper.*;
import com.woniuxy.service.ResidentsService;
import com.woniuxy.util.ResponseEnum;
import com.woniuxy.util.ResponseUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 居民信息服务实现类
 * 实现居民信息相关的业务操作
 */
@Service
public class ResidentsServiceImpl implements ResidentsService {
    @Resource
    private ResidentsMapper residentsMapper;
    @Resource
    private HealthInfoMapper healthInfoMapper;
    @Resource
    private BloodPressureRecordsMapper bloodPressureRecordsMapper;
    @Resource
    private BloodGlucoseRecordsMapper bloodGlucoseRecordsMapper;
    @Resource
    private WeightRecordsMapper weightRecordsMapper;
    @Resource
    private HeartRateRecordsMapper heartRateRecordsMapper;

    /**
     * 查询所有居民信息
     * @return 返回所有居民信息的响应结果
     */
    @Override
    public ResponseUtil queryAllShow(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Residents> residents = residentsMapper.queryAllShow();
        residents.forEach(resident -> {
            if (resident.getDiseasesString() != null) {
                resident.setDiseases(Arrays.asList(resident.getDiseasesString().split("、")));
            } else {
                resident.setDiseases(new ArrayList<>());
            }
        });
        PageInfo<Residents> residentsPageInfo = new PageInfo<>(residents);
        return ResponseUtil.get(ResponseEnum.OK,residentsPageInfo);
    }

    /**
     * 根据居民ID查询居民信息
     * @param residentId 居民ID
     * @return 返回指定居民ID的居民信息的响应结果
     */
    @Override
    public ResponseUtil queryByResidentId(Integer residentId) {
        Residents residents = residentsMapper.queryByResidentId(residentId);
        return ResponseUtil.get(ResponseEnum.OK,residents);
    }

    /**
     * 根据居民ID更新居民信息（删除操作）
     * @param residentId 居民ID
     * @return 返回更新居民信息的响应结果
     */
    @Override
    public ResponseUtil updateForDelete(Integer residentId) {
        Integer integer = residentsMapper.updateForDelete(residentId);
        return ResponseUtil.get(ResponseEnum.OK);
    }

    /**
     * 更新居民信息
     * @param residents 居民信息对象
     * @return 返回更新居民信息的响应结果
     */
    @Override
    public ResponseUtil update(Residents residents) {
        residentsMapper.update(residents);
        return ResponseUtil.get(ResponseEnum.OK);
    }

    /**
     * 根据居民ID查询居民详细信息
     * @param residentId 居民ID
     * @return 返回指定居民ID的详细信息的响应结果
     */
    @Override
    public ResponseUtil queryAllDetails(Integer residentId) {
        Residents residents = residentsMapper.queryByResidentId(residentId);
        HealthInfo healthInfo = healthInfoMapper.queryByResidentId(residentId);
        List<BloodGlucoseRecords> bloodGlucoseRecords = bloodGlucoseRecordsMapper.queryRecent(residentId);
        List<BloodPressureRecords> bloodPressureRecords = bloodPressureRecordsMapper.queryRecent(residentId);
        List<WeightRecords> weightRecords = weightRecordsMapper.queryRecent(residentId);
        List<HeartRateRecords> heartRateRecords = heartRateRecordsMapper.queryRecent(residentId);
        ResidentsHealthRecord residentsHealthRecord = new ResidentsHealthRecord(residents, healthInfo, bloodGlucoseRecords, bloodPressureRecords, weightRecords, heartRateRecords);
        return ResponseUtil.get(ResponseEnum.OK,residentsHealthRecord);
    }

    /**
     * 根据居民ID查询最近一次的居民信息
     * @param residentId 居民ID
     * @return 返回指定居民ID的最近一次信息的响应结果
     */
    @Override
    public ResponseUtil queryRecentOne(Integer residentId) {
        BloodGlucoseRecords bloodGlucoseRecords = bloodGlucoseRecordsMapper.queryRecentOne(residentId);
        BloodPressureRecords bloodPressureRecords = bloodPressureRecordsMapper.queryRecentOne(residentId);
        HeartRateRecords heartRateRecords = heartRateRecordsMapper.queryRecentOne(residentId);
        WeightRecords weightRecords = weightRecordsMapper.queryRecentOne(residentId);
        ResidentHealthRecordLatest residentHealthRecordLatest = new ResidentHealthRecordLatest(bloodGlucoseRecords, bloodPressureRecords, heartRateRecords, weightRecords);
        return ResponseUtil.get(ResponseEnum.OK,residentHealthRecordLatest);

    }
    
    /**
     * 根据登录ID查询居民信息
     * @param loginId 登录ID
     * @return 查询结果
     */
    @Override
    public ResponseUtil queryById(Integer loginId) {
        Residents resident = residentsMapper.queryByResidentId(loginId);
        if (resident == null){
            return ResponseUtil.get(ResponseEnum.LOGIN_USER_NOT_EXIST);
        }else {
            return ResponseUtil.get(ResponseEnum.OK,resident);
        }
    }

    /**
     * 更新居民信息
     * @param resident 居民信息
     * @return 更新结果
     */
    @Override
    public ResponseUtil updateResident(Residents resident) {
        Integer flag = residentsMapper.update(resident);
        if (flag > 0){
            return ResponseUtil.get(ResponseEnum.UPDATE_USER_OK);
        }else {
            return ResponseUtil.get(ResponseEnum.FAIL);
        }
    }

    /**
     * 根据类型获取居民的医疗历史
     * @param residentId 居民ID
     * @param type 类型
     * @return 响应结果
     */
    @Override
    public ResponseUtil getHealthHistoryByType(Integer residentId, String type) {
        Object data = null;
        switch (type) {
            case "血糖":
                data = bloodGlucoseRecordsMapper.queryRecent(residentId);
                break;
            case "血压":
                data = bloodPressureRecordsMapper.queryRecent(residentId);
                break;
            case "体重":
                data = weightRecordsMapper.queryRecent(residentId);
                break;
            case "心率":
                data = heartRateRecordsMapper.queryRecent(residentId);
                break;
            default:
                return ResponseUtil.get(ResponseEnum.FAIL, "未知的指标类型");
        }
        return ResponseUtil.get(ResponseEnum.OK, data);
    }

    /**
     * 根据居民ID查询服务团队的成员
     * @param residentId 居民ID
     * @return 返回该居民所属服务团队的成员列表
     */
    @Override
    public ResponseUtil queryServiceTeamMembersByResidentId(Integer residentId) {
        List<SysDoctor> teamMembers = residentsMapper.queryServiceTeamMembersByResidentId(residentId);
        if (teamMembers == null || teamMembers.isEmpty()) {
            return ResponseUtil.get(ResponseEnum.OK, "该居民暂无服务团队");
        }
        return ResponseUtil.get(ResponseEnum.OK, teamMembers);
    }

    /**
     * 根据医生ID查询其所在团队管理的所有患者
     * @param doctorId 医生ID
     * @return 返回居民列表
     */
    @Override
    public ResponseUtil queryResidentsByDoctorId(Integer doctorId) {
        List<Residents> residents = residentsMapper.queryResidentsByDoctorId(doctorId);
        residents.forEach(resident -> {
            if (resident.getDiseasesString() != null) {
                resident.setDiseases(Arrays.asList(resident.getDiseasesString().split("、")));
            } else {
                resident.setDiseases(new ArrayList<>());
            }
        });
        return ResponseUtil.get(ResponseEnum.OK, residents);
    }
}
